<template>
  <el-tree :data="regionList" :props="defaultProps" :load="load" lazy />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { GetRegionByPcode } from '@/api/region'
//地区数据列表
const regionList = ref([])
//懒加载是的查询code
const selectCode = ref(0)
const loadData = async pcode => {
  const { data } = await GetRegionByPcode(pcode)
  regionList.value = data
}

onMounted(() => {
  loadData(0)
})
//懒加载
const load = async (node, resolve) => {
  // console.log(node);
  if (node.data.code) {
    selectCode.value = node.data.code
  }
  const { data } = await GetRegionByPcode(selectCode.value)
  data.forEach(item => {
    item.leaf = item.level == 3 ? true : false
  })
  resolve(data)
}

const defaultProps = {
  children: 'children',
  label: 'name',
  isLeaf: 'leaf',
}
</script>
